﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace EncuentraloSilverlight
{
    public static class Cookie
    {

       public static string GetCookie(string key)
        {

            string[] cookies = HtmlPage.Document.Cookies.Split(';');

            for (int i = 0; i < cookies.Length; i++)
            {
                if (cookies[i].StartsWith(" "))
                    cookies[i] = cookies[i].Remove(0, 1);
            }

            foreach (string cookie in cookies)
            {

                string[] keyValue = cookie.Split('=');

                if (keyValue.Length == 2)
                {
                    if (keyValue[0].ToString() == key)

                        return keyValue[1];
                }

            }

            return null;
        }

       public static void SetCookie(string key, string value)
        {

            // Expire in 7 days

            DateTime expireDate = DateTime.Now + TimeSpan.FromDays(7);


            string newCookie = key + "=" + value + ";expires=" + expireDate.ToString("R");

            HtmlPage.Document.SetProperty("cookie", newCookie);

        }

    }
}
